/*
 * Copyright 2008-2009 Aying.Net Powered by Fuchun.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.aying.util;

import java.text.DateFormat;
import java.util.Date;
import java.util.Calendar;
import java.util.Locale;

import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

/**
 * @author Fuchun
 * @version 1.0, 2009-6-25
 */
public class DateUtilsTestCase extends UtilBasedTestCase {

    private Date date1, date2, date3, date4, date5, date6, date7, date8, date9, date10, date11,
            date12;

    public DateUtilsTestCase(String testName) {
        super(testName);
    }

    public void setUp() throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.set(2009, Calendar.JANUARY, 25);
        // 2009-1-25 Sun.
        date1 = cal.getTime();
        cal.set(2009, Calendar.FEBRUARY, 23);
        // 2009-2-23 Mon.
        date2 = cal.getTime();
        cal.set(2009, Calendar.MARCH, 24);
        // 2009-3-24 Tue.
        date3 = cal.getTime();
        cal.set(2009, Calendar.APRIL, 22);
        // 2009-4-24 Wed.
        date4 = cal.getTime();
        cal.set(2009, Calendar.MAY, 21);
        // 2009-5-21 Thu.
        date5 = cal.getTime();
        cal.set(2009, Calendar.JUNE, 26);
        // 2009-6-26 Fri.
        date6 = cal.getTime();
        cal.set(2009, Calendar.JULY, 25);
        // 2009-7-25 Set.
        date7 = cal.getTime();
        cal.set(2009, Calendar.AUGUST, 1);
        // 2009-8-1
        date8 = cal.getTime();
        cal.set(2009, Calendar.SEPTEMBER, 1);
        // 2009-9-1
        date9 = cal.getTime();
        cal.set(2009, Calendar.OCTOBER, 1);
        // 2009-10-1
        date10 = cal.getTime();
        cal.set(2009, Calendar.NOVEMBER, 1);
        // 2009-11-1
        date11 = cal.getTime();
        cal.set(2009, Calendar.DECEMBER, 1);
        // 2009-12-1
        date12 = cal.getTime();
    }

    public void tearDown() throws Exception {
        date1 = null;
        date2 = null;
        date3 = null;
        date4 = null;
        date5 = null;
        date6 = null;
        date7 = null;
    }

    /** @see DateUtils#getWeekDay(java.util.Date) */
    public void test_getWeekDay() throws Exception {
        assertEquals(DateUtils.WEEK_NAMES[6], DateUtils.getWeekDay(date1));
        assertEquals(DateUtils.WEEK_NAMES[0], DateUtils.getWeekDay(date2));
        assertEquals(DateUtils.WEEK_NAMES[1], DateUtils.getWeekDay(date3));
        assertEquals(DateUtils.WEEK_NAMES[2], DateUtils.getWeekDay(date4));
        assertEquals(DateUtils.WEEK_NAMES[3], DateUtils.getWeekDay(date5));
        assertEquals(DateUtils.WEEK_NAMES[4], DateUtils.getWeekDay(date6));
        assertEquals(DateUtils.WEEK_NAMES[5], DateUtils.getWeekDay(date7));
    }
    
    /** @see DateUtils#getMonth(java.util.Date) */
    public void test_getMonth() throws Exception {
        assertEquals(DateUtils.MONTH_NAMES[0], DateUtils.getMonth(date1));
        assertEquals(DateUtils.MONTH_NAMES[1], DateUtils.getMonth(date2));
        assertEquals(DateUtils.MONTH_NAMES[2], DateUtils.getMonth(date3));
        assertEquals(DateUtils.MONTH_NAMES[3], DateUtils.getMonth(date4));
        assertEquals(DateUtils.MONTH_NAMES[4], DateUtils.getMonth(date5));
        assertEquals(DateUtils.MONTH_NAMES[5], DateUtils.getMonth(date6));
        assertEquals(DateUtils.MONTH_NAMES[6], DateUtils.getMonth(date7));
        assertEquals(DateUtils.MONTH_NAMES[7], DateUtils.getMonth(date8));
        assertEquals(DateUtils.MONTH_NAMES[8], DateUtils.getMonth(date9));
        assertEquals(DateUtils.MONTH_NAMES[9], DateUtils.getMonth(date10));
        assertEquals(DateUtils.MONTH_NAMES[10], DateUtils.getMonth(date11));
        assertEquals(DateUtils.MONTH_NAMES[11], DateUtils.getMonth(date12));
    }
    
    /**
     * @see DateUtils#getShortDate()
     * @see DateUtils#getShortDate(Locale)
     */
    public void test_getShortDate() throws Exception {
    	Date now = new Date();
    	DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
    	String str_now1 = df1.format(now);
    	assertEquals(str_now1, DateUtils.getShortDate());
    	DateFormat df2 = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CANADA);
    	String str_now2 = df2.format(now);
    	assertEquals(str_now2, DateUtils.getShortDate(Locale.CANADA));
    }
    
    /**
     * @see DateUtils#getLongDate()
     * @see DateUtils#getLongDate(Locale)
     */
    public void test_getLongDate() throws Exception {
    	Date now = new Date();
    	DateFormat df1 = DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault());
    	String str_now1 = df1.format(now);
    	assertEquals(str_now1, DateUtils.getLongDate());
    	DateFormat df2 = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
    	String str_now2 = df2.format(now);
    	assertEquals(str_now2, DateUtils.getLongDate(Locale.FRANCE));
    }
    
    /**
     * @see DateUtils#getShortTime()
     * @see DateUtils#getShortTime(Locale)
     */
    public void test_getShortTime() throws Exception {
    	Date now = new Date();
    	DateFormat df1 = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
    	String str_now1 = df1.format(now);
    	assertEquals(str_now1, DateUtils.getShortTime(Locale.getDefault()));
    	DateFormat df2 = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.FRANCE);
    	String str_now2 = df2.format(now);
    	assertEquals(str_now2, DateUtils.getShortTime(Locale.FRANCE));
    }
    
    /**
     * @see DateUtils#getLongTime()
     * @see DateUtils#getLongTime(Locale)
     */
    public void test_getLongTime() throws Exception {
    	Date now = new Date();
    	DateFormat df1 = DateFormat.getTimeInstance(DateFormat.LONG, Locale.getDefault());
    	String str_now1 = df1.format(now);
    	assertEquals(str_now1, DateUtils.getLongTime(Locale.getDefault()));
    	DateFormat df2 = DateFormat.getTimeInstance(DateFormat.LONG, Locale.FRANCE);
    	String str_now2 = df2.format(now);
    	assertEquals(str_now2, DateUtils.getLongTime(Locale.FRANCE));
    }
    
    

    public static Test suite() {
        return new TestSuite(DateUtilsTestCase.class);
    }

    public static void main(String[] args) {
        TestRunner.run(suite());
    }
}
